﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes {
    import Engine.Classes.*;
    import flash.display.*;
    import flash.geom.*;
    import Engine.Helpers.*;
    import de.polygonal.ds.*;

	/**
	 * 对比ObjectLayer，多维护了一个dirtyObjectList, 另外在放入Object时的查找算法也有差别
	 */
    public class GameObjectLayer extends ObjectLayer {

        private static var debugCounter:int = 0;

        private var m_dirtyObjectList:DLinkedList;

        public function GameObjectLayer(_arg1:String, _arg2:Number=0){
            this.m_dirtyObjectList = new DLinkedList();
            super(_arg1, _arg2);
        }
        public function clearDirtyObjectList():void{
            this.m_dirtyObjectList.clear();
        }
        public function get dirty():Boolean{
            return (((this.m_dirtyObjectList.size > 0)) ? true : false);
        }
        public function get dirtyObjectList():DLinkedList{
            return (this.m_dirtyObjectList);
        }
        override public function get children():Array{
            return (m_children);
        }
        override public function cleanUp():void{
            var _local3:WorldObject;
            var _local1:Array = m_children.concat([]);
            var _local2:int;
            while (_local2 < _local1.length) {
                _local3 = (_local1[_local2] as WorldObject);
                if (_local3 != null){
                    _local3.detatch();
                    _local3.cleanUp();
                };
                _local2++;
            };
            m_displayObject = new Sprite();
        }
        override public function attach():void{
            var _local1:IsoViewport = (GlobalEngine.viewport as IsoViewport);
            _local1.addObjectLayer(this);
        }
        override public function detach():void{
            var _local1:IsoViewport = (GlobalEngine.viewport as IsoViewport);
            _local1.removeObjectLayer(this);
        }
        override public function render(_arg1:RenderContext):void{
            var _local2:int = m_children.length;
            var _local3:int;
            while (_local3 < _local2) {
                m_children[_local3].render(_arg1);
                _local3++;
            };
        }
        public function postRender():void{
            this.clearDirtyObjectList();
        }
        override public function insertObjectIntoDepthArray(_arg1:WorldObject, _arg2:String=null):void{
            var _local3:int = this.findInsertionPoint(_arg1);
            m_children.splice(_local3, 0, _arg1);
            var _local4:DisplayObject = _arg1.getDisplayObject();
            var _local5:Sprite = (this.getDisplayObject() as Sprite);
            if (_local4 == null){
                _local5.addChildAt(_arg1.createDisplayObject(), _local3);
            } else {
                if (_local4.parent == null){
                    _local5.addChildAt(_local4, _local3);
                } else {
                    _local4.parent.setChildIndex(_local4, _local3);
                };
            };
        }
        override public function updateObjectInDepthArray(_arg1:WorldObject):void{
            var _local3:int;
            var _local4:int;
            var _local2:DisplayObject = _arg1.getDisplayObject();
            if (((((_local2) && (_local2.parent))) && (_arg1.isAttached()))){
                _local3 = m_children.indexOf(_arg1);
                if ((((_local3 >= 0)) && ((_local3 < m_children.length)))){
                    m_children.splice(_local3, 1);
                };
                _local4 = this.findInsertionPoint(_arg1);
                m_children.splice(_local4, 0, _arg1);
                _local2.parent.setChildIndex(_local2, _local4);
            } else {
                if (_arg1.isAttached()){
                    this.insertObjectIntoDepthArray(_arg1);
                };
            };
            this.m_dirtyObjectList.append(_arg1);
        }
        private function findInsertionPoint(_arg1:PositionedObject):int{
            var _local6:PositionedObject;
            var _local2:int;
            var _local3:int = m_children.length;
            var _local4 = ((_local2 + _local3) >> 1);
            var _local5:int;
            while (_local2 < _local3) {
                _local6 = (m_children[_local4] as PositionedObject);
                _local5 = this.sortObjects(_arg1, _local6);
                if (_local5 == -1){
                    _local3 = _local4;
                    _local4 = ((_local2 + _local3) >> 1);
                } else {
                    if (_local5 == 1){
                        _local2 = (_local4 + 1);
                        _local4 = ((_local2 + _local3) >> 1);
                    } else {
                        if (_local5 == 0){
                            break;
                        };
                    };
                };
            };
            return (_local4);
        }
        override public function checkObjectDepthLocation(_arg1:DisplayObject, _arg2:int):Boolean{
            return ((_arg1 == _arg1.parent.getChildAt(_arg2)));
        }
        protected function sortObjects(_arg1:Object, _arg2:Object):int{
            var _local3:int;
            var _local4:int;
            var _local5:WorldObject = (_arg1 as WorldObject);
            var _local6:WorldObject = (_arg2 as WorldObject);
            var _local7:Vector3 = _local5.getDepthPositionNoClone();
            var _local8:Vector3 = _local6.getDepthPositionNoClone();
            var _local9:Vector3 = _local5.getEndPosition();
            var _local10:Vector3 = _local6.getEndPosition();
            if (_local6.getDepthPriority() > _local5.getDepthPriority()){
                _local4 = -1;
            } else {
                if (_local6.getDepthPriority() < _local5.getDepthPriority()){
                    _local4 = 1;
                };
            };
            if ((((((((_local8.x >= _local9.x)) && ((((_local8.y < _local9.y)) && ((_local10.y > _local7.y)))))) || ((((_local8.y >= _local9.y)) && ((((_local8.x < _local9.x)) && ((_local10.x > _local7.x)))))))) && ((_local4 == 0)))){
                _local3 = 1;
            } else {
                if ((((((((_local7.x >= _local10.x)) && ((((_local7.y < _local10.y)) && ((_local9.y > _local8.y)))))) || ((((_local7.y >= _local10.y)) && ((((_local7.x < _local10.x)) && ((_local9.x > _local8.x)))))))) && ((_local4 == 0)))){
                    _local3 = -1;
                } else {
                    if (_local6.depthIndex > _local5.depthIndex){
                        _local3 = 1;
                    } else {
                        if (_local6.depthIndex < _local5.depthIndex){
                            _local3 = -1;
                        };
                    };
                };
            };
            return (_local3);
        }
        override public function removeObjectFromDepthArray(_arg1:WorldObject):void{
            var _local3:DisplayObject;
            var _local2:int = m_children.indexOf(_arg1);
            if ((((_local2 >= 0)) && ((_local2 < m_children.length)))){
                m_children.splice(_local2, 1);
                _local3 = _arg1.getDisplayObject();
                if (((_local3) && (_local3.parent))){
                    _local3.parent.removeChild(_local3);
                };
            };
        }
        override public function pickObject(_arg1:Point, _arg2:uint=16777215, _arg3:uint=0):PositionedObject{
            var _local4:int;
            var _local6:PositionedObject;
            var _local5:PositionedObject;
            _local4 = (m_children.length - 1);
            while (_local4 >= 0) {
                _local6 = (m_children[_local4] as PositionedObject);
                _local5 = (_local6.pickObject(_arg1, _arg2, _arg3) as PositionedObject);
                if (_local5 != null){
                    break;
                };
                _local4--;
            };
            return (_local5);
        }

    }
}//package Classes 
